﻿using System.Configuration;
using System.IO;

namespace TWA.TFS
{
    /// <summary>
    /// Utility for logging messages. Log files are written to the configured location.
    /// </summary>
    internal static class Logger
    {
        private static StreamWriter logWriter;
        private static string logLevel = ConfigurationManager.AppSettings ["LogLevel"];
        private static string logDirectory = ConfigurationManager.AppSettings ["LogDirectory"];
        private static string fileName = ConfigurationManager.AppSettings ["LogFileName"];

        internal static void Log (string logString)
        {
            if (logLevel.ToLowerInvariant () != "on")
            {
                return;
            }

            if (string.IsNullOrEmpty (fileName))
            {
                return;
            }

            logWriter = new StreamWriter (Path.Combine (logDirectory, fileName), true);
            
            logWriter.WriteLine (logString);
            logWriter.Flush ();

            logWriter.Close ();
        }
    }
}